<template>
  <div>
    <div>你有关注消息</div>
    <div>{{this.message}}</div>
  </div>
</template>

<script>
export default {
  name: "received",
  data(){
    return{
      message:'',
      path:"ws://localhost:8002/WebSocketController/zhangsan",
      socket: {}
    }
  },
  mounted () {
    // 初始化
    this.init()
  },
  methods:{
    init() {
      if(typeof(WebSocket) === "undefined"){
        alert("您的浏览器不支持socket")
      }else{
        // 实例化socket
        this.socket = new WebSocket(this.path)
        // 监听socket连接
        this.socket.onopen = this.open
        // 监听socket错误信息
        this.socket.onerror = this.error
        // 监听socket消息
        this.socket.onmessage = function(result){
          console.log(result)
        }
      }
    },
    open(msg) {
      console.log("socket连接成功")
      console.log(msg)
    },
    error: function () {
      console.log("连接错误")
    },
    getMessage: function (msg) {
      this.message=msg.data
    },
    send: function () {
      this.socket.send(params)
    },
    close: function () {
      console.log("socket已经关闭")
    }
  }

}
</script>

<style scoped>

</style>
